package org.birtserver.backend.dao.reports;

import java.util.List;

import org.birtserver.backend.model.Report;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

public class ReportDaoImpl extends SimpleJdbcDaoSupport implements ReportDao {

  private String queryCreateNewReport;
  
  private String queryGetAllReports;

  private String queryDeleteReport;

  private String queryUpdateReport;

  private String queryGetCompleteReport;

  @Override
  public void createNewReport(Report report) {
    
    new NamedParameterJdbcTemplate(getDataSource()).update(
        queryCreateNewReport,
        new BeanPropertySqlParameterSource(report));
        
  }

  @Override
  public void deleteReport(long reportId) {
    
    this.getSimpleJdbcTemplate().update(
        queryDeleteReport,
        new Object[] { reportId });

  }

  @Override
  public List<Report> getAllReportNames() {
    
    return this.getSimpleJdbcTemplate().query(
        queryGetAllReports,
        ParameterizedBeanPropertyRowMapper.newInstance(Report.class),
        new Object[] { });
    
  }
  
  @Override
  public Report getCompleteReport(long reportId) {
    
    return this.getSimpleJdbcTemplate().queryForObject(
        queryGetCompleteReport,
        ParameterizedBeanPropertyRowMapper.newInstance(Report.class),
        new Object[] { reportId });
  }

  @Override
  public void updateReport(Report report) {
    
    new NamedParameterJdbcTemplate(getDataSource()).update(
        queryUpdateReport,
        new BeanPropertySqlParameterSource(report));

  }
  
  @Required
  public void setQueryCreateNewReport(String queryCreateNewReport) {
    this.queryCreateNewReport = queryCreateNewReport;
  }

  @Required
  public void setQueryGetAllReports(String queryGetAllReports) {
    this.queryGetAllReports = queryGetAllReports;
  }
  
  @Required
  public void setQueryDeleteReport(String queryDeleteReport) {
    this.queryDeleteReport = queryDeleteReport;
  }
  
  @Required
  public void setQueryUpdateReport(String queryUpdateReport) {
    this.queryUpdateReport = queryUpdateReport;
  }
  
  @Required
  public void setQueryGetCompleteReport(String queryGetCompleteReport) {
    this.queryGetCompleteReport = queryGetCompleteReport;
  }
  
}
